package mapgenerator;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;


/*
 * ProjectDialog.java
 *
 * Created on Feb 15, 2009, 8:32:01 PM
 *
 * @author Sajarwo Anggai
 */
public class ProjectDialog extends JDialog implements ActionListener, KeyListener {

    private Main comp;
    private JButton Browse;
    private JButton Cancel;
    private JButton Create;
    private JLabel jLabelBackgrond;
    private JLabel jLabelMH;
    private JLabel jLabelMW;
    private JLabel jLabelPF;
    private JLabel jLabelPL;
    private JLabel jLabelPN;
    private JLabel jLabelTH;
    private JLabel jLabelTW;
    private JTextField jTextFieldMapHeight;
    private JTextField jTextFieldMapWidth;
    private JTextField jTextFieldPF;
    private JTextField jTextFieldPL;
    private JTextField jTextFieldPN;
    private JTextField jTextFieldTileHeight;
    private JTextField jTextFieldTileWidth;
    private File dir;
    private Project project;
    private File checkDir;
    private int mw;
    private int mh;
    private int tw;
    private int th;

    public ProjectDialog(Main comp) {
        this.comp = comp;
        this.project = comp.project;
        initComponents();
    }

    private void initComponents() {
        jLabelPN = new JLabel();
        jLabelPL = new JLabel();
        jLabelPF = new JLabel();
        jLabelMW = new JLabel();
        jLabelMH = new JLabel();
        jLabelTW = new JLabel();
        jLabelTH = new JLabel();
        jTextFieldPN = new JTextField();
        jTextFieldPL = new JTextField();
        jTextFieldPF = new JTextField();
        jTextFieldMapWidth = new JTextField("20", 5);
        jTextFieldMapHeight = new JTextField("20", 5);
        jTextFieldTileWidth = new JTextField("32", 3);
        jTextFieldTileHeight = new JTextField("32", 3);
        Browse = new JButton();
        Cancel = new JButton();
        Create = new JButton();
        jLabelBackgrond = new JLabel();


        setTitle("Create New Project");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(510, 320));
        setLocationByPlatform(true);
        setResizable(false);
        getContentPane().setLayout(new AbsoluteLayout());

        jLabelPN.setText("Project Name:");
        getContentPane().add(jLabelPN, new AbsoluteConstraints(10, 60, -1, -1));

        jLabelPL.setText("Project Location:");
        getContentPane().add(jLabelPL, new AbsoluteConstraints(10, 90, -1, -1));

        jLabelPF.setText("Project Folder:");
        getContentPane().add(jLabelPF, new AbsoluteConstraints(10, 120, -1, -1));

       
        jLabelMW.setText("Map Width:");
        getContentPane().add(jLabelMW, new AbsoluteConstraints(110, 160, -1, -1));

        jLabelMH.setText("Map Height:");
        getContentPane().add(jLabelMH, new AbsoluteConstraints(110, 190, -1, -1));

        jLabelTW.setText("Tile Width:");
        getContentPane().add(jLabelTW, new AbsoluteConstraints(280, 160, -1, -1));

        jLabelTH.setText("Tile Height:");
        getContentPane().add(jLabelTH, new AbsoluteConstraints(280, 190, -1, -1));

        jTextFieldPN.addKeyListener(this);
        jTextFieldMapWidth.addKeyListener(this);
        jTextFieldMapWidth.setToolTipText("Minimum map width is 1");
        jTextFieldMapHeight.addKeyListener(this);
        jTextFieldMapHeight.setToolTipText("Minimum map height is 1");
        jTextFieldTileWidth.addKeyListener(this);
        jTextFieldTileWidth.setToolTipText("Minimum tile width is 5");
        jTextFieldTileHeight.addKeyListener(this);
        jTextFieldTileHeight.setToolTipText("Minimum tile height is 5");
        jTextFieldPF.setEditable(false);
        jTextFieldPL.setEditable(false);
        getContentPane().add(jTextFieldPN, new AbsoluteConstraints(110, 60, 380, -1));
        getContentPane().add(jTextFieldPL, new AbsoluteConstraints(110, 90, 300, -1));
        getContentPane().add(jTextFieldPF, new AbsoluteConstraints(110, 120, 380, -1));
        getContentPane().add(jTextFieldMapWidth, new AbsoluteConstraints(180, 160, 60, -1));
        getContentPane().add(jTextFieldMapHeight, new AbsoluteConstraints(180, 190, 60, -1));
        getContentPane().add(jTextFieldTileWidth, new AbsoluteConstraints(350, 160, 60, -1));
        getContentPane().add(jTextFieldTileHeight, new AbsoluteConstraints(350, 190, 60, -1));

        Browse.setText("Browse");
        Browse.addActionListener(this);
        getContentPane().add(Browse, new AbsoluteConstraints(420, 90, -1, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(this);
        getContentPane().add(Cancel, new AbsoluteConstraints(350, 240, -1, -1));

        Create.setText("Create");
        Create.setEnabled(false);
        Create.addActionListener(this);
        getContentPane().add(Create, new AbsoluteConstraints(430, 240, -1, -1));

        jLabelBackgrond.setIcon(new ImageIcon(getClass().getResource("/com/images/ProjectDialogBackground.png"))); // NOI18N
        getContentPane().add(jLabelBackgrond, new AbsoluteConstraints(0, 0, 510, 320));

        pack();
    }

    /**
     * Listener untuk button Browse dan Create serta Cancel
     * @param evt
     */
    public void actionPerformed(ActionEvent evt) {
        Object ob = evt.getSource();
        if (ob == Browse) {
            JFileChooser jf = new JFileChooser();
            jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jf.showOpenDialog(this);
            dir = jf.getSelectedFile();
            if (dir != null) {
                jTextFieldPL.setText(dir.getAbsolutePath());
                validateButtonCreate();
            }
        } else if (ob == Create) {
            JOptionPane jop = new JOptionPane();
            if (checkDir.exists()) {
                JOptionPane.showMessageDialog(this, "Folder for your project directory has exist\nPlease try again!!!");
            } else {
                synchronized (checkDir) {
                    checkDir.mkdir();
                    File imageDir = new File(checkDir.getPath() + File.separator + "images");
                    imageDir.mkdir();
                    try {
                        checkDir.wait(500);
                    } catch (InterruptedException ex) {
                        //Logger.getLogger(ProjectDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    System.out.println(checkDir.getAbsolutePath());
                    System.out.println(imageDir.getAbsolutePath());
                    project.setProjectName(checkDir.getName());
                    project.setPath(checkDir.getAbsolutePath());
                    project.setImageDir(imageDir.getAbsolutePath());
                    project.setMapWidth(mw);
                    project.setMapHeight(mh);
                    project.setTileWidth(tw);
                    project.setTileHeight(th);
                    project.setCreated(true);
                    comp.lmControlPanel.gridWorkspace.drawGrid();
                    comp.ScrollPaneWorkspaceRender.setVisible(true);
                    comp.WorkspaceRender.setPreferredSize(new Dimension(mw * tw, mh * th));
                    comp.WorkspaceRender.revalidate();
                    comp.repaint();
                }
                JOptionPane.showMessageDialog(this, "Project has been created sucessfully!!!");
                this.dispose();
            }
        } else {
            this.dispose();
        }
    }

    private void validateButtonCreate() {
        if (dir != null) {
            String pathName = dir.getAbsolutePath();
            if (pathName.endsWith("\\")) {
                pathName += jTextFieldPN.getText().trim();
            } else {
                pathName += "\\" + jTextFieldPN.getText().trim();
            }
            jTextFieldPF.setText(pathName);
            checkDir = new File(pathName);
        }

        try {
            mw = Integer.parseInt(jTextFieldMapWidth.getText());
            mh = Integer.parseInt(jTextFieldMapHeight.getText());
            tw = Integer.parseInt(jTextFieldTileWidth.getText());
            th = Integer.parseInt(jTextFieldTileHeight.getText());

            if (mw < 1 || mh < 1 || tw < 5 || th < 5 || jTextFieldPL.getText().equals("") || jTextFieldPN.getText().trim().equals("") || checkDir.exists()) {
                Create.setEnabled(false);
            } else {
                Create.setEnabled(true);
            }
        } catch (NumberFormatException e) {
            Create.setEnabled(false);
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent evt) {
        validateButtonCreate();
    }
}